package com.cskaoyan.service;

import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminTopicGoodsListVo;
import com.cskaoyan.bean.vo.AdminTopicVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author chengyi
 * @date 2022/10/13 20:38
 */
@Service
public class AdminTopicServiceImpl implements AdminTopicService {
    @Autowired
    MarketTopicMapper topicMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    /**
     * 查找专题
     *
     * @param info
     * @param title
     * @param subtitle
     * @return
     */
    @Override
    public CommonData<MarketTopic> list(BasePageInfo info, String title, String subtitle) {
        //开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        //创建实例
        MarketTopicExample example = new MarketTopicExample();
        //排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketTopicExample.Criteria criteria = example.createCriteria();
        //判空
        if (!StringUtil.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtil.isEmpty(subtitle)) {
            criteria.andTitleLike("%" + subtitle + "%");
        }

        List<MarketTopic> list = topicMapper.selectByExample(example);
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    /**
     * 插入专题
     *
     * @param marketTopic
     * @return
     */
    @Override
    public MarketTopic create(MarketTopic marketTopic) {

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        java.util.Date date1 = null;
        try {
            date1 = formatter.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketTopic.setAddTime(date1);
        marketTopic.setUpdateTime(date1);
        //插入专题到数据库
        topicMapper.insertSelective(marketTopic);
        int id = marketTopic.getId();
        MarketTopic data = topicMapper.selectByPrimaryKey(id);
        return data;
    }

    @Override
    public AdminTopicVo read(Integer id) {
        //获取topic
        MarketTopic marketTopic = topicMapper.selectByPrimaryKey(id);
        AdminTopicVo adminTopicVo = new AdminTopicVo();
        //获取goodsId
        String[] goods = marketTopic.getGoods();
        if(goods.length == 0){
            adminTopicVo.setTopic(marketTopic);
            adminTopicVo.setGoodsList(null);
        }else {
            String gid = goods[0];
            Integer goodsId = Integer.parseInt(gid);

            //获取goodsList
            List<AdminTopicGoodsListVo> marketGoods = topicMapper.selectByKey(goodsId);

            //把值放进去
            adminTopicVo.setTopic(marketTopic);
            adminTopicVo.setGoodsList(marketGoods);
        }
        return adminTopicVo;
    }

    /**
     * 修改专题
     *
     * @param marketTopic
     * @return
     */
    @Override
    public MarketTopic update(MarketTopic marketTopic) {

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        java.util.Date date1 = null;
        try {
            date1 = formatter.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer id = marketTopic.getId();
        marketTopic.setUpdateTime(date1);
        topicMapper.updateByPrimaryKey(marketTopic);

        MarketTopic data = topicMapper.selectByPrimaryKey(id);
        return data;
    }

    /**
     * 删除专题
     *
     * @param marketTopic
     */
    @Override
    public void delete(MarketTopic marketTopic) {
        Integer id = marketTopic.getId();
        topicMapper.deleteByPrimaryKey(id);
    }

    /**
     * 循环删除
     *
     * @param
     */
    @Override
    public void batchDelete(Map map) {
        List<Integer> ids = (List<Integer>) map.get("ids");
        for (Integer id : ids) {
           topicMapper.deleteByPrimaryKey(id);

        }


    }
}
